/**
 * Created by gongxun on 16/11/24.
 */

var KwxTableRoomEndLayer = GameCCSBase.extend({
    Panel_players: null,
    mJuEndedEventkwx : null,
    ctor: function () {
        this._super();
        this.loadCocostudio("res/ccs/KwxTableRoomEnd.json");
        mo.gameHelper.fixIosText(this.ccsNode);
        this.Panel_players = [];

        mo.seekWidgetByName(this.ccsNode, "Button_guanbi").addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)) {
                // mo.kwxTable.quitKwxGame();
                if (mo.sceneHelper.currentLayerName == "login"){
                    //在登录界面就会带登录
                    mo.sceneHelper.intoLoginLayer();
                }else{
                    mo.sceneHelper.runSceneHall();
                }
                mo.sceneHelper.closeEndInfoLayer();
            }
        }, this);

        mo.seekWidgetByName(this.ccsNode, "Button_ok").addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)) {
                mo.gameHelper.onWxShareFriend(mo.gameHelper.projectName + "棋牌-牌局结算-卡五星", "我完成了一个" + mo.gameHelper.projectName + "棋牌的牌局，这个是结算截图！");
            }
        }, this);

        this.loadUI();

        this.setVisible(false);

        mo.gameHelper.setTextString(mo.seekWidgetByName(this.ccsNode, "Text_gameName"), mo.gameHelper.projectName + "卡五星");

        //2人场调整布局
        if (mo.gameMsgManager.kwxGameModel == 2){
            var width = mo.seekWidgetByName(this.ccsNode, "Panel_player").getContentSize().width;
            this.Panel_players[0].setPositionX(width * 0.3);
            this.Panel_players[1].setPositionX(width * 0.7);
            this.Panel_players[2].setPositionX(width * 0.7);
        }
    },

    onEnter : function(){
        this._super();

        mo.eventHelper.add("PushRYScore", this, function(PushRYScore){
        });
    },

    onExit : function(){
        mo.eventHelper.remove(this);
        this._super();
    },

    loadUI: function () {
        for (var i = 0; i < 3; i++) {
            var Panel_player = mo.seekWidgetByName(this.ccsNode, "Image_player" + i);
            this.Panel_players.push(Panel_player);
        }
    },

    setRoomEndInfo : function(juEndedEventkwx, myUid){
        this.mJuEndedEventkwx = juEndedEventkwx;

        //房间规则
        var ruleJson = JSON.parse(juEndedEventkwx.CfgJson);
        var rule = "游戏规则：" + mo.gameHelper.getRuleConfigSummary(ruleJson, "kwx", juEndedEventkwx.TableId);
        mo.gameHelper.setTextString(mo.seekWidgetByName(this.ccsNode, "Text_rule"), rule);

        //房间号
        var tableIdStr = "" + juEndedEventkwx.TableId;
        mo.seekWidgetByName(this.ccsNode, "AtlasLabel_roomId").setString(tableIdStr);
        // mo.gameHelper.setTextString(mo.seekWidgetByName(this.ccsNode, "Text_room"), tableIdStr);

        //时间
        var date = new Date(juEndedEventkwx.EndTime * 1000);
        // var time = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate() + "\n";
        // time += (date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds());
        // mo.gameHelper.setTextString(mo.seekWidgetByName(this.ccsNode, "Text_time"), time);
        mo.gameHelper.setTextString(mo.seekWidgetByName(this.ccsNode, "Text_time"), mo.gameHelper.getTimeYYMMDD(date));

        //游戏玩法
        var Image_wanfa = mo.seekWidgetByName(this.ccsNode, "Image_wanfa");
        var wanfaImageFile = [
            "res/ccs/commonEnd/wanfa_xg.png",
            "res/ccs/commonEnd/wanfa_xy.png",
            "res/ccs/commonEnd/wanfa_sy.png",
            "res/ccs/commonEnd/wanfa_sz.png"
        ];
        mo.gameHelper.loadTextureImage(Image_wanfa, wanfaImageFile[ruleJson.ruleid - 1]);

        //游戏局数
        mo.gameHelper.setTextString(mo.seekWidgetByName(this.ccsNode, "Text_jushu"), (parseInt(ruleJson.maxpan) - juEndedEventkwx.LeftPlayTimes) + "/" + ruleJson.maxpan);

        //大赢家
        var bigWinIndex = 0;
        var tempSocore = 0;

        //按照分数排序
        var juInfos = juEndedEventkwx.Ju.slice(0);
        juInfos.sort(function(a, b){
            return b.Score - a.Score;
        });

        for (var i = 0; i < 3; i++){
            var player = mo.seekWidgetByName(this.ccsNode, "Image_player" + i);
            var juInfo = juInfos[i];
            if (juInfo == null){
                player.setVisible(false);
                continue;
            }
            player.setVisible(true);

            //nick
            mo.gameHelper.setTextString(mo.seekWidgetByName(player, "Text_nick"), mo.gameHelper.getUserNickFilterForCount(juInfo.Nick, 4));

            //uid
            mo.gameHelper.setTextString(mo.seekWidgetByName(player, "Text_uid"), juInfo.Uid);

            //head
            mo.gameHelper.loadImageForUrl(mo.seekWidgetByName(player, "Image_head"), juInfo.FaceUrl);

            //房主
            mo.seekWidgetByName(player, "Image_create").setVisible(juInfo.Uid == juEndedEventkwx.CreateUid);

            //info
            var infoStr = "";
            infoStr += ("自摸次数:             " + juInfo.Zm);
            infoStr += ("\n接炮次数:             " + juInfo.Paoed);
            infoStr += ("\n点炮次数:             " + juInfo.Pao);
            infoStr += ("\n暗杠次数:             " + juInfo.Gang2);
            infoStr += ("\n明杠次数:             " + juInfo.Gang1);
            infoStr += ("\n加飘得分:             " + juInfo.Piao);
            mo.gameHelper.setTextString(mo.seekWidgetByName(player, "Text_info"), infoStr);

            //score
            var Fnt_score = mo.seekWidgetByName(player, "Fnt_score");
            var scoreStr = juInfo.Score;
            if (scoreStr >= 0){
                Fnt_score.setFntFile("res/ccs/commonEnd/winFnt.fnt");
                scoreStr = "+" + scoreStr;
            }else {
                Fnt_score.setFntFile("res/ccs/commonEnd/loseFnt.fnt");
            }
            Fnt_score.setString(scoreStr);

            //socore
            if (juInfo.Score > tempSocore){
                tempSocore = juInfo.Score;
                bigWinIndex = i;
            }

            //是否赢了
            mo.seekWidgetByName(player, "Image_isWin").loadTexture(juInfo.Score >= 0 ? "res/ccs/commonEnd/img_win.png" : "res/ccs/commonEnd/img_lose.png");
        }

        //大赢家
        for (var i = 0; i < 3; i++){
            var player = mo.seekWidgetByName(this.ccsNode, "Image_player" + i);
            var Image_bigWinner = mo.seekWidgetByName(player, "Image_bigWinner");
            Image_bigWinner.setVisible(i == bigWinIndex);
        }

        //礼券
        var Text_liquan = mo.seekWidgetByName(this.ccsNode, "Text_liquan");
        Text_liquan.setVisible(false);
        for (var i = 0; i < juEndedEventkwx.Ju.length; i++){
            if (juEndedEventkwx.Ju[i].Uid == myUid){
                if (juEndedEventkwx.Ju[i].Coupon != null && juEndedEventkwx.Ju[i].Coupon != 0){
                    mo.gameHelper.setTextString(Text_liquan, "+" + juEndedEventkwx.Ju[i].Coupon);
                    Text_liquan.setVisible(true);
                }
            }
        }

        //结束原因
        var Text_exitInfo = mo.seekWidgetByName(this.ccsNode, "Text_exitInfo");
        if (juEndedEventkwx.EndFlag == 0){
            mo.gameHelper.setTextString(Text_exitInfo, "自然结束");
        }else if (juEndedEventkwx.EndFlag == 1){
            mo.gameHelper.setTextString(Text_exitInfo, "用户投票结束");
        }else if (juEndedEventkwx.EndFlag == 2){
            mo.gameHelper.setTextString(Text_exitInfo, "生死战结束,生死战封顶:" + ruleJson.lod);
        }
    }
});
